package com.rp.user.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rp.news.common.PagedResult;
import com.rp.news.common.RedisCommon;
import com.rp.news.enums.Sex;
import com.rp.news.util.CommonUtil;
import com.rp.news.util.RedisOperator;
import com.rp.pojo.AppUser;
import com.rp.pojo.Fans;
import com.rp.user.mapper.FansMapper;
import com.rp.user.service.MyFanService;
import com.rp.user.service.UserService;
import com.rp.vo.RegionRatioVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.FastCharsetProvider;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyFanServiceImpl implements MyFanService {

    @Autowired
    private RedisOperator redis;

    @Autowired
    private FansMapper fansMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private CommonUtil commonUtil;

    @Override
    public boolean isMeFollowThisWriter(String writerId, String fanId) {

        Fans fans = new Fans();
        fans.setWriterId(writerId);
        fans.setFanId(fanId);
        int result = fansMapper.selectCount(fans);
        return result > 0;
    }

    @Override
    public boolean follow(String writerId, String fanId) {
        AppUser user = userService.getUser(fanId);

        Fans fans =new Fans();
        fans.setId(IdUtil.fastSimpleUUID());
        fans.setFanId(fanId);
        fans.setWriterId(writerId);

        fans.setFace(user.getFace());
        fans.setFanNickname(user.getNickname());
        fans.setSex(user.getSex());
        fans.setProvince(user.getProvince());

        int insert = fansMapper.insert(fans);

        //作家的粉丝数加一
        redis.increment(RedisCommon.REDIS_WRITER_FANS_COUNTS+writerId,1);

        //当前用户的关注数累加1
        redis.increment(RedisCommon.REDIS_MY_FOLLOW_COUNTS+writerId,1);
        return insert >0;
    }

    @Override
    public void unfollow(String writerId, String fanId) {
        Fans fans =new Fans();
        fans.setFanId(fanId);
        fans.setWriterId(writerId);

        fansMapper.delete(fans);
        //作家的粉丝数加一
        redis.decrement(RedisCommon.REDIS_WRITER_FANS_COUNTS+writerId,1);

        //当前用户的关注数累加1
        redis.decrement(RedisCommon.REDIS_MY_FOLLOW_COUNTS+writerId,1);
    }

    @Override
    public PagedResult queryMyFansList(String writerId, Integer page, Integer pageSize) {

        Fans fans = new Fans();

        fans.setWriterId(writerId);
        PageHelper.startPage(page,pageSize);
        List<Fans> select = fansMapper.select(fans);
        return commonUtil.setterPageResult(select,page);
    }

    @Override
    public Integer queryFansCounts(String writerId, Sex sex) {
        Fans fans = new Fans();
        fans.setWriterId(writerId);
        fans.setSex(sex.type);
        int i = fansMapper.selectCount(fans);
        return i;
    }


    public static final String[] regions = {"北京", "天津", "上海", "重庆",
            "河北", "山西", "辽宁", "吉林", "黑龙江", "江苏", "浙江", "安徽", "福建", "江西", "山东",
            "河南", "湖北", "湖南", "广东", "海南", "四川", "贵州", "云南", "陕西", "甘肃", "青海", "台湾",
            "内蒙古", "广西", "西藏", "宁夏", "新疆",
            "香港", "澳门"};



    @Override
    public List<RegionRatioVO> queryReginRationCounts(String writerId) {
        Fans fans = new Fans();
        fans.setWriterId(writerId);

        List<RegionRatioVO> list = new ArrayList<>();
        for (String r : regions) {
            fans.setProvince(r);
            Integer count = fansMapper.selectCount(fans);

            RegionRatioVO regionRatioVO = new RegionRatioVO();
            regionRatioVO.setName(r);
            regionRatioVO.setValue(count);

            list.add(regionRatioVO);
        }

        return list;
    }

}
